/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.web.cm.auth.openid;

import static cc.aileron.wsgi.navigation.WsgiNavigation.State.NotFound;
import static cc.aileron.wsgi.navigation.WsgiNavigation.State.Redirect;

import java.util.List;

import org.openid4java.consumer.ConsumerManager;
import org.openid4java.discovery.DiscoveryException;
import org.openid4java.discovery.DiscoveryInformation;
import org.openid4java.message.AuthRequest;

import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.navigation.WsgiNavigation;

import com.google.inject.Inject;

/**
 * 認証要求を行います
 * @author Aileron
 *
 */
public class OpenIdAuthProcess implements WsgiProcess<OpenIdIdentifier>
{
    @Inject
    private OpenIdAuthContainer container;
    
    /**
     * consumerManager
     */
    private final ConsumerManager consumerManager;
    
    /**
     * constractor injection
     * @param consumerManager
     */
    @Inject
    public OpenIdAuthProcess(final ConsumerManager consumerManager)
    {
        this.consumerManager = consumerManager;
    }
    
    @Override
    public WsgiNavigation doProcess(final OpenIdIdentifier identifier) 
    throws Exception
    {
        final String url = associate(discover(identifier.stringValue)); 
        if(url==null)
        {
            return NotFound.navigation();
        }
        return Redirect.navigation(url);
    }
    
    /**
     * consumerManager.discover(identifier)
     * @param identifier
     * @return
     */
    @SuppressWarnings("unchecked")
    private List discover(final String identifier)
    {
        try
        {
            return consumerManager.discover(identifier);
        }
        catch(DiscoveryException e)
        {
            return null;
        }
    }
    
    /**
     * consumerManager.associate(discoveries)
     * @param discoveries
     */
    @SuppressWarnings("unchecked")
    private String associate(List discoveries)
    {
        if(discoveries==null)
        {
            return null ;
        }
        final DiscoveryInformation discovered = 
            consumerManager.associate(discoveries);
        container.set(discovered);
        final String returnURL="http://localhost:8080/cm/veryfy";
        try
        {
            final AuthRequest authRequest = 
                consumerManager.authenticate(discovered, returnURL);
            return authRequest.getDestinationUrl(true);
        }
        catch(Exception e)
        {
            return null;
        }
    }
}